package com.siblings.loan.ui.loan.impl

import android.content.Context
import com.siblings.loan.http.IHttpCallback
import com.siblings.loan.status.StatusRepository
import com.siblings.loan.ui.loan.view.ISibLoanLayerView
import com.siblings.loan.utils.event.SibEventData
import com.siblings.loan.utils.helper.SharedHelper
import com.siblings.mvp.presenter.MvpPresenter
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/4 15:46
 * @Annotation：主页面 Presenter 实现
 */
class SibLoanLayerPresenterImpl(context: Context?) : MvpPresenter<ISibLoanLayerView>(context) {
    private var model: SibLoanLayerModelImpl? = null
    init {
        model = SibLoanLayerModelImpl()
    }

    /**
     * 获取总流程
     */
    fun loadSibProcessAction(){
        model!!.loadSibProcessAction(object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        //保存用户流程状态
                        if (obj != null) {
                            // TODO: 测试添加 Step 类型
//                            val allStepTitles = obj.optJSONArray("allStepTitles")
//                            allStepTitles.put("Ocr Info")
//                            allStepTitles.put("Face recognition")
//                            allStepTitles.put("Account Info")
//                            val allSteps = obj.optJSONArray("allSteps")
//                            allSteps.put("ocr_info")
//                            allSteps.put("sefie_info")
//                            allSteps.put("account_info")
//                            val steps = obj.optJSONArray("steps")
//                            val index = Random().nextInt(3)
//                            when(index){
//                                0 -> {
//                                    steps.put("ocr_info")
//                                }
//                                1 -> {
//                                    steps.put("sefie_info")
//                                }
//                                else -> {
//                                    steps.put("account_info")
//                                }
//                            }

                            SharedHelper.saveSibSPData(SharedHelper.SIB_SPKEY.SIBLINGS_PROCESS,obj.toString())
                            //通知全局流程刷新完成
                            EventBus.getDefault().post(SibEventData(SibEventData.TYPE.SIB_PROCESS_REFRESHED))
                        }
                        //隐藏所有页面
                        mvpView.hideSibAllLoanPage()

                        //1、判断是否存在 订单状态
                        if (obj != null && obj.has("orderStatus")){
                            val orderStatus = obj.optString("orderStatus")
                            //待还款
                            if (StatusRepository(null,orderStatus).isRepayNow()){
                                obj.put("eventRemark","LoanAudit Page")
                                mvpView.showSibLoanLimitPage()
                                return
                            }
                            //放款中
                            if (StatusRepository(null,orderStatus).isDisbursing()){
                                obj.put("eventRemark","LoanAudit Page")
                                mvpView.showSibLoanCommonPage(obj)
                                return
                            }
                            //申请贷款被拒绝
                            if (StatusRepository(null,orderStatus).isRejected()){
                                obj.put("eventRemark","LoanApplyFailed Page")
                                mvpView.showSibLoanCommonPage(obj)
                                return
                            }
                        }

                        //2、判断是否存在 贷款申请状态
                        if (obj != null && obj.has("applyStatus")){
                            val applyStatus = obj.optString("applyStatus")
                            //审核中
                            if (StatusRepository(applyStatus).isReviewing()){
                                obj.put("eventRemark","LoanAudit Page")
                                mvpView.showSibLoanCommonPage(obj)
                                return
                            }
                            //被拒绝
                            if (StatusRepository(applyStatus).isRejected()){
                                obj.put("eventRemark","LoanApplyFailed Page")
                                mvpView.showSibLoanCommonPage(obj)
                                return
                            }
                        }

                        //3、直接显示贷款额度页面
                        if (obj != null){
                            if (obj.optJSONArray("steps")!!.length() == 0){//流程已完成 - 显示贷款额度
                                mvpView.showSibLoanLimitPage()
                            } else {//未授信
                                mvpView.showSibUncreditedPage()
                            }
                        }
                    }
                    else -> {
                        mvpView.showSibToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView.showSibToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView.hideSibLoading()
            }
        })
    }
}